package com.example.demo.nuoqi.controller;

import com.example.demo.nuoqi.pojo.Problemmanagement;
import com.example.demo.nuoqi.pojo.User;
import com.example.demo.nuoqi.service.ProblemmanagementService;
import com.example.demo.nuoqi.utils.Result;
import com.example.demo.nuoqi.utils.StatusCode;
import com.example.demo.nuoqi.utils.UserUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "ProblemmanagementController")
@RestController
@RequestMapping("/problemmanagement")
@CrossOrigin
public class ProblemmanagementController {

    @Autowired
    private ProblemmanagementService problemmanagementService;

    /***
     * Problemmanagement分页条件搜索实现
     * @param problemmanagement
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Problemmanagement条件分页查询",notes = "分页条件查询Problemmanagement方法详情",tags = {"ProblemmanagementController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Problemmanagement对象",value = "传入JSON数据",required = false) Problemmanagement problemmanagement, @PathVariable int page, @PathVariable int size){
        //调用ProblemmanagementService实现分页条件查询Problemmanagement
        PageInfo<Problemmanagement> pageInfo = problemmanagementService.findPage(problemmanagement, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Problemmanagement分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Problemmanagement分页查询",notes = "分页查询Problemmanagement方法详情",tags = {"ProblemmanagementController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size){
        //调用ProblemmanagementService实现分页查询Problemmanagement
        PageInfo<Problemmanagement> pageInfo = problemmanagementService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     *
     * @param problemmanagement
     * @return
     */
    @ApiOperation(value = "Problemmanagement条件查询",notes = "条件查询Problemmanagement方法详情",tags = {"ProblemmanagementController"})
    @PostMapping(value = "/search" )
    public Result<List<Problemmanagement>> findList(@RequestBody(required = false) @ApiParam(name = "Problemmanagement对象",value = "传入JSON数据",required = false) Problemmanagement problemmanagement){
        //调用ProblemmanagementService实现条件查询Problemmanagement
        List<Problemmanagement> list = problemmanagementService.findList(problemmanagement);
        return new Result<List<Problemmanagement>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Problemmanagement根据ID删除",notes = "根据ID删除Problemmanagement方法详情",tags = {"ProblemmanagementController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用ProblemmanagementService实现根据主键删除
        problemmanagementService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Problemmanagement数据
     * @param problemmanagement
     * @param id
     * @return
     */
    @ApiOperation(value = "Problemmanagement根据ID修改",notes = "根据ID修改Problemmanagement方法详情",tags = {"ProblemmanagementController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Problemmanagement对象",value = "传入JSON数据",required = false) Problemmanagement problemmanagement, @PathVariable Integer id){
        //设置主键值
        problemmanagement.setId(id);
        //调用ProblemmanagementService实现修改Problemmanagement
        problemmanagementService.update(problemmanagement);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Problemmanagement数据
     * @param problemmanagement
     * @return
     */
    @ApiOperation(value = "Problemmanagement添加",notes = "添加Problemmanagement方法详情",tags = {"ProblemmanagementController"})
    @PostMapping
    public Result add(@RequestBody @ApiParam(name = "Problemmanagement对象",value = "传入JSON数据",required = true) Problemmanagement problemmanagement){
        //调用ProblemmanagementService实现添加Problemmanagement
        problemmanagementService.add(problemmanagement);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Problemmanagement数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Problemmanagement根据ID查询",notes = "根据ID查询Problemmanagement方法详情",tags = {"ProblemmanagementController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<Problemmanagement> findById(@PathVariable Integer id){
        //调用ProblemmanagementService实现根据主键查询Problemmanagement
        Problemmanagement problemmanagement = problemmanagementService.findById(id);
        return new Result<Problemmanagement>(true,StatusCode.OK,"查询成功",problemmanagement);
    }

    /***
     * 查询Problemmanagement全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Problemmanagement",notes = "查询所Problemmanagement有方法详情",tags = {"ProblemmanagementController"})
    @GetMapping
    public Result<List<Problemmanagement>> findAll(){
        //调用ProblemmanagementService实现查询所有Problemmanagement
        List<Problemmanagement> list = problemmanagementService.findAll();
        return new Result<List<Problemmanagement>>(true, StatusCode.OK,"查询成功",list) ;
    }



    /***
     * 查询当前用户所有问题
     * @return
     */
    @ApiOperation(value = "查询当前用户所有问题",notes = "查询当前用户所有问题",tags = {"ProblemmanagementController"})
    @GetMapping("/findByUserProble")
    public Result<List<Problemmanagement>> findByUserProble(HttpSession session){
        //调用ProblemmanagementService实现查询所有Problemmanagement

        User user = UserUtils.getUser(session);


        List<Problemmanagement> list = problemmanagementService.findAll();
        List<Problemmanagement> temp = new ArrayList<>();

        for (Problemmanagement problemmanagement : list) {
            if (problemmanagement.getUserId() == user.getId()) {
                temp.add(problemmanagement);
            }
        }
        return new Result<List<Problemmanagement>>(true, StatusCode.OK,"查询当前用户问题成功",temp) ;
    }



    /***
     * 设置问题状态
     * @return
     */
    @ApiOperation(value = "设置问题状态",notes = "设置问题状态",tags = {"ProblemmanagementController"})
    @GetMapping("setProbleResult/{problesId}/{result}")
    public Result setProbleResult(@PathVariable("problesId")Integer id,@PathVariable("result")String result){
        Problemmanagement byId = problemmanagementService.findById(id);
        byId.setStatus(result);
        problemmanagementService.update(byId);
        return new Result(true, StatusCode.OK,"问题状态修改成功",byId) ;
    }
}
